# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: 

inherit eutils

MY_P=${P/_rc/rc}
S=${WORKDIR}/${MY_P}

DESCRIPTION="NetAMS Traffic Accounting"
SRC_URI="http://www.netams.com/files/${MY_P}.tar.gz"
SLOT="0"
LICENSE="GPL-2"
KEYWORDS="x86 amd64"
RDEPEND="dev-db/mysql net-libs/libpcap"


src_unpack() {
    unpack ${MY_P}.tar.gz
    #epatch ${FILESDIR}/${MY_P}-configure.sh-gentoo.patch || die
}

src_compile() {
    ./configure.sh
    FLAGS=-DHAVE_BW make
}

PATH_TO_WWW="/var/netams"

src_install() {
    einfo "Installing NetAMS"

    exeinto /usr/sbin
    doexe src/flowprobe src/ulog2netflow src/netams src/netamsctl # src/ipfw2netflow

    insinto /usr/share/man/man8
    doins doc/*.8 || die "NetAMS install failed"

    sed -i -e "s#PATH_TO_WWW#${PATH_TO_WWW}#g" addon/netams.conf
    insinto /etc
    doins addon/netams.conf addon/.netamsctl.rc addon/ru-networks.txt
    
    newinitd addon/netams-gentoo.sh netams || die
    
    exeinto /usr/libexec/netams
    doexe addon/*.pl
    
    einfo "Installing NetAMS WWW part"
    
    insinto ${PATH_TO_WWW}
    doins addon/index.html
    
    insinto ${PATH_TO_WWW}/images
    doins cgi-bin/images/*.gif cgi-bin/images/*.jpg
    
    exeinto ${PATH_TO_WWW}/cgi-bin
    doexe cgi-bin/*.cgi cgi-bin/*.pl cgi-bin/*.tmpl cgi-bin/*.res
    
    insinto ${PATH_TO_WWW}/cgi-bin/admin
    doins cgi-bin/admin/.htaccess
    
    keepdir ${PATH_TO_WWW}/stat
    
    mkdir -p ${D}/etc/apache2/modules.d
    cat > ${D}/etc/apache2/modules.d/99_netams.conf << EOF
# $Id: netams-apache,v 1.1 2008-01-02 13:31:38 anton Exp $
<IfDefine NAGIOS>
        ScriptAlias /netams/cgi-bin/ ${PATH_TO_WWW}/cgi-bin/
        <Directory "${PATH_TO_WWW}/cgi-bin">
                Options Indexes FollowSymLinks MultiViews ExecCGI
                AllowOverride none
                Order allow,deny
                allow from all
                DefaultLanguage ru
                AddDefaultCharset WINDOWS-1251
        </Directory>
        Alias /netams ${PATH_TO_WWW}/
        <Directory "${PATH_TO_WWW}/">
                Options Indexes FollowSymLinks MultiViews
                AllowOverride none
                Order allow,deny
                allow from all
                DefaultLanguage ru
                AddDefaultCharset WINDOWS-1251
        </Directory>
</IfDefine>
EOF
    chmod 0644 ${D}/etc/apache2/modules.d/99_netams.conf
    
    dodoc doc/README* doc/TODO.txt \
        addon/netams-apache addon/access-script-linux.README addon/subnet-sum.c addon/*.sql 
}

pkg_postinst() {
    echo
    elog "NeTAMS is now installed."
    elog 
    elog "Sample configuration file is /etc/netams/netams.conf.sample"
    elog "Edit, rename it and check startup at /etc/init.d/netams"
    elog
    elog "Web interface and CGI scripts are in ${PATH_TO_WWW}"
    elog "Apache Web Server configuration file is at /etc/apache2/modules.d/99_netams.conf"
    elog
    elog "PLEASE READ THE DOCUMENTATION!"
    elog "http://www.netams.com"
    echo
}
